package javaTec.nearestPoint;

import java.util.*;

/**
 * 给定一些 points 和一个 origin，从 points 中找到 k 个离 origin 最近的点。
 * 按照距离由小到大返回。如果两个点有相同距离，则按照x值来排序；
 * 若x值也相同，就再按照y值排序。
 * Created by yangshunxing on 2017/8/31.
 */
public class NearestPoint {
    public Point[] kClosest(Point[] points, Point origin, int k) {
        // Write your code here
        Map<Double, Point> map = new HashMap<>();
        Arrays.stream(points).forEach(point -> map.put(Math.sqrt(point.x*point.x+point.y*point.y),point));
        List<Double> keys = new ArrayList<>();
        for(Double key: map.keySet()){
            keys.add(key);
        }
        List<Double> sortKeys = new ArrayList<>();
        while(keys.size()>0){
            Double smaller = getSmaller(keys);
            sortKeys.add(smaller);
            keys.remove(smaller);
        }
        return null; 
    }

    private Double getSmaller(List<Double> keys) {
        Double smaller;
        smaller = keys.get(0);
        for (int i=0; i< keys.size();i++){
            if(smaller>keys.get(i+1)){
                smaller = keys.get(i+1);
            }
        }
        return smaller;
    }
}
